package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        Item item = Item.builder().build();
        item.setId(id);
        item.setStatus(status);
        this.getBaseMapper().updateById(item);
        String routKey = status == 1? "item.up":"item.down";
        rabbitTemplate.convertAndSend("item.topic",routKey,id);
    }
    @Transactional
    @Override
    public void deductStock(Long itemId, Integer num) {
        try {
            // update tb_item set stock = stock - #{num} where id = #{itemId}
            update().setSql("stock = stock - " + num).eq("id", itemId).update();
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }
    }
}
